package com.yaodian100.crm.activities.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.crm.activities.entity.PromotionPresent;

public class PromotionPresentServiceImp extends DomainServiceImpl<PromotionPresent> implements PromotionPresentService {
	@Resource
	private BaseManager baseMgr;

	@Override
	@Transactional(readOnly = false)
	public String savegift(PromotionPresent promotionPresent) {
		baseMgr.save(promotionPresent);
		return null;
	}

	@Override
	public List<PromotionPresent> findByPrId(String prId) {

		List<PromotionPresent> list = new ArrayList<PromotionPresent>();

		Object[] type = baseMgr.queryByHql("from PromotionPresent where prId=? ", new Object[] { prId });
		if (type.length > 0) {

			for (Object object : type) {

				PromotionPresent couponItem = (PromotionPresent) object;

				list.add(couponItem);

			}
		}
		return list;

	}

	@Override
	public String updateAfnjQty(PromotionPresent promotionPresent) {
		baseMgr.update(promotionPresent);
		return null;
	}

	@Override
	public String updateGiftAfnjQty(Map<String, Object> giftMap) {
		PromotionPresent promotionPresent = new PromotionPresent();

		Object[] type = baseMgr.queryByHql("from PromotionPresent where prId=? and specId=? ",
				new Object[] { giftMap.get("giftId"), giftMap.get("giftspeid") });
		if (type.length > 0) {

			for (Object object : type) {

				promotionPresent = (PromotionPresent) object;

			}
			promotionPresent.setAfnjQty(promotionPresent.getAfnjQty() + 1);
			baseMgr.update(promotionPresent);

		}
		return null;
	}

}
